#include "Universal.h"
#include <windows.h>
#include <iostream>

void Universal::OutputTxt(const char *text, const bool line)
{
    if (text && *text)
    {
        printf("%s%s", text, line ? "\n" : "");
    }
    else
    {
        printf(line ? "\n" : "");
    }
}

void Universal::OutputBool(const int value, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%s%s%s", pre_txt ? pre_txt : "", value ? "true" : "false", post_txt ? post_txt : "", line ? "\n" : "");
}

void Universal::OutputDec(const int64 value, const char *pre_text, const char *post_text, const bool line)
{
    printf("%s%I64d%s%s", pre_text ? pre_text : "", value, post_text ? post_text : "", line ? "\n" : "");
}

void Universal::OutputFloat(const double value, const char *pre_text, const char *post_text, const bool line)
{
    printf("%s%5.2lf%s%s", pre_text ? pre_text : "", value, post_text ? post_text : "", line ? "\n" : "");
}

void Universal::OutputHex02(const byte value, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%02X%s%s", pre_txt ? pre_txt : "", value, post_txt ? post_txt : "", line ? "\n" : "");
}

void Universal::OutputHex04(const ushort value, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%04X%s%s", pre_txt ? pre_txt : "", value, post_txt ? post_txt : "", line ? "\n" : "");
}

void Universal::OutputHex08(const uint value, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%08X%s%s", pre_txt ? pre_txt : "", value, post_txt ? post_txt : "", line ? "\n" : "");
}

void Universal::OutputHex16(const uint64 value, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%016I64X%s%s", pre_txt ? pre_txt : "", value, post_txt ? post_txt : "", line ? "\n" : "");
}

void Universal::OutputIpv4(const uint ip, const char *pre_txt, const char *post_txt, const bool line)
{
    printf("%s%d.%d.%d.%d%s%s", pre_txt ? pre_txt : "", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF, post_txt ? post_txt : "",
        line ? "\n" : "");
}

void Universal::OutputErr(const KSError error, const char *func_name, const char *comment)
{
    if (error == KS_OK) {return;}

    HANDLE console_output = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO buffer_info;
    GetConsoleScreenBufferInfo(console_output, &buffer_info);

    SetConsoleTextAttribute(console_output, FOREGROUND_RED | FOREGROUND_INTENSITY);
    const char *chr_error;
    KS_getErrorString(error, &chr_error, KSLNG_DEFAULT);
    printf("ERROR (0x%08X = \'%s\') -\n  %s: %s\n", error, chr_error, func_name, comment);

    SetConsoleTextAttribute(console_output, buffer_info.wAttributes);
    if (KS_getErrorDescription(error, func_name, &chr_error, KSLNG_DEFAULT) == KS_OK)
    {
        OutputTxt(chr_error);
    }
}

